package com.tianqu.tms.www.transfer.action;

import com.tianqu.LicensePlateConfigConstant;
import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsAction;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsDaoException;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.*;
import com.tianqu.tms.www.transfer.actionform.TransferRgAdvanceSearchInputActionForm;
import com.tianqu.tms.www.transfer.viewhelper.TransferRgAdvanceInputViewHelper;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 待办受理查询结果页面Action
 * 
 * @author Administrator
 * 
 */
public class TransferRgAdvanceSearchResultAction extends TmsAction {
	private Logger logger = Logger.getLogger(TransferRgAdvanceSearchResultAction.class);
	@Override
	protected ActionForward doAction(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		TransferRgAdvanceSearchInputActionForm inputForm = (TransferRgAdvanceSearchInputActionForm) form;
		String serial = inputForm.getSerial();
		String licensePlate = inputForm.getLicensePlate();
		if (LicensePlateConfigConstant.defLicensePlate.equals(licensePlate)) {
			licensePlate = "";
		}
		if ((serial == null || "".equals(serial))
				&& (licensePlate == null || "".equals(licensePlate))) {
			setErrorsToRequest(request, TmsConstants.IM005, null);
			return mapping.getInputForward();
		}
		TransferAcceptedInfo accept = new TransferAcceptedInfo();
		TransferRgAdvanceInputViewHelper view = new TransferRgAdvanceInputViewHelper();
		Codes code = Codes.getInstance();
		accept = accept.getAcceptInfoBySerial(serial,licensePlate);
		String actionForward = "";
		if (accept != null) {
			
			TransferFinanceInfo finance = new TransferFinanceInfo();
			finance = finance.findFinanceInfoByserial(accept.getSerial());
			if(finance.getTransferPriceDate()!=null)
			{
				view.setTransferPrice(String.valueOf(finance.getTransferPrice()));
				view.setTransferPriceS(String.valueOf(finance.getTransferPrice())+"（已缴费）");
			}else{
				view.setTransferPrice("");
				view.setTransferPriceS("（未缴费）");
			}
			
				view.setStatus(code.getItemValueByItemIdAndItemCode(Codes.ACCEPT_STATUS, accept.getStatus()));
				view.setSerial(accept.getSerial());// 流水号
				view.setLicensePlate(accept.getLicensePlate());// 车牌号码
				view.setVin(accept.getVin());// vin码
				view.setBasicCarId(accept.getBasicCarId());
				view.setAgentName(accept.getAgentName());
				view.setAgentOwnerIdCardNo(accept.getAgentOwnerIdCardNo());
				view.setAgentAddress(accept.getAgentAddress());
				view.setOwnersGroupPhoto(accept.getOwnersGroupPhoto());
				view.setMaterialImages1(accept.getMaterialImages1());
				view.setMaterialImages2(accept.getMaterialImages2());
				view.setMaterialImages3(accept.getMaterialImages3());
				view.setMaterialImages4(accept.getMaterialImages4());
				view.setMaterialImages5(accept.getMaterialImages5());
				view.setAgentPhone(accept.getAgentPhone());
				if (accept.getVin() == null || "".equals(accept.getVin())) {
					view.setVinbsflag("1");
				} else {
					view.setVinbsflag("2");
				}
				TransferProcessHistory his = new TransferProcessHistory();
				view.setLcList(his.loadtHistoryBySerial(accept.getSerial()));
				if (accept.getStatus() == 3) {
					if (accept.getMarketType() == 1) {
						Car car = Car.getInstance();
						car = car.findCarBySerial(accept.getBasicCarId());
						if (car != null) {
							view = toViewHelper(car,accept, view);
							if(accept.getOwnerType() == 0)
							{
								view.setOwnerType("1");
								view.setOwnerTypeName("");
							}else{
								view.setOwnerType(String.valueOf(accept.getOwnerType()));
								view.setOwnerTypeName(code.getItemValueByItemIdAndItemCode(Codes.CAR_OWNER, accept.getOwnerType()));
							}
						}
						//modfiy by huangheli 20150917 start
//						actionForward = "marketCar";
						//modfiy by huangheli 20150917 end
					}
					if (accept.getMarketType() == 2) {
						TransferPersonalCarInfo person = new TransferPersonalCarInfo();
						person = person.getPersonalCarInfoBySerial(accept.getBasicCarId());
						view.setBasicCarId(accept.getBasicCarId());
						view = toViewHelper1(person,accept, view);
						if(accept.getOwnerType() == 0)
						{
							view.setOwnerType("1");
							view.setOwnerTypeName("");
						}else{
							view.setOwnerType(String.valueOf(accept.getOwnerType()));
							view.setOwnerTypeName(code.getItemValueByItemIdAndItemCode(Codes.CAR_OWNER, accept.getOwnerType()));
						}					
						view.setCarScore("7"); 
						//modfiy by huangheli 20150917 end
						//actionForward = "personalCar";
						//modfiy by huangheli 20150917 end
					}
					//add by huangheli 20150917 start
					actionForward = "marketCar";
					//add by huangheli 20150917 end
				} else {
					setErrorsToRequest(request, TmsConstants.IM006, null);
					return mapping.getInputForward();
				}	
		} else {
			setErrorsToRequest(request, TmsConstants.IM006, null);
			return mapping.getInputForward();
		}

		uc.setAttribute(TmsConstants.VIEWHELP, view);
		logger.debug(this.getClass().getName() + "#execute was completed.");
		return mapping.findForward(actionForward);
	}

	/**
	 * entity -> viewhelper
	 * 
	 * @param entity
	 * @return
	 * @throws TmsDaoException
	 */
	private TransferRgAdvanceInputViewHelper toViewHelper(Car entity,TransferAcceptedInfo accept,
			TransferRgAdvanceInputViewHelper view) throws TmsDaoException {


		view.setBrandId(entity.getBrandId());
		view.setVendorId(entity.getVendorId());
		view.setCarStyleId(entity.getCarStyleId());
		view.setCardType(Integer.toString(entity.getCardType()));
		view.setCarType(Integer.toString(entity.getCarType()));
		view.setCarUse(Integer.toString(entity.getCarUse()));
		view.setColor(Integer.toString(entity.getColor()));
		view.setCompanyContactPerson(entity.getCompanyContactPerson());
		if(entity.getCountryStyle() < 0)
		{
			view.setCountryStyle("");
		}else{
			view.setCountryStyle(Integer.toString(entity.getCountryStyle()));
		}
		view.setEngine(entity.getEngine());

		view.setModelCode(entity.getModelCode());
		view.setOwnerAddress(entity.getOwnerAddress());
		view.setOwnerPhone(entity.getOwnerPhone());
		view.setCarOwner(entity.getCarOwner());
		view.setCarOwnerIdCardNo(entity.getCarOwnerIdCardNo());
		view.setOwnerOrgNo(entity.getOwnerOrgNo());

		view.setRegDate(entity.getRegDate());
		view.setRegisterCardNo(entity.getRegisterCardNo());
		view.setSendCardDate(entity.getSendCardDate());
		view.setVendorCode(entity.getVendorCode());
		view.setVin(entity.getVin());
		
		Member member = Member.getInstance();
		//modify by huangheli 20150917 start
		//view.setMemberName(member.getMemNameByMemId(entity.getMemberId()));
		//view.setMemberId(String.valueOf(entity.getMemberId()));
		if(!StringUtil.isNullOrBlank(accept.getMemberId())){
		view.setMemberName(member.getMemNameByMemId(Integer.valueOf(accept.getMemberId())));
		view.setMemberId(accept.getMemberId());
		}
		//modify by huangheli 20150917 end
		view.setCarScore(String.valueOf(entity.getCarScore()));
		
		if(entity.getVendorId()!=null && !"".equals(entity.getVendorId()))
		{
			//车辆品牌
			Vendor vendor = Vendor.getInstance();
			CarStyle carStyle = CarStyle.getInstance();
			VehicleBrand brand = VehicleBrand.getInstance();
			CommercialVehicle commercialVehicle = CommercialVehicle.getInstance();
			String str = entity.getVendorId().substring(0,1);
			if("S".equals(str))
			{
				view.setFromType(1);
				view.setVendorList(vendor.findAllVendorInfo());
				view.setCarStyleList(carStyle.getAllCarStyleInfo(entity.getBrandId()));
				view.setBrandList(brand.getAllVehicleBrandInfo(entity.getVendorId()));
			}else{
				view.setFromType(2);
				view.setVendorList(commercialVehicle.findAllVendorInfo());
				view.setCarStyleList(commercialVehicle.getAllStyleInfo(entity.getBrandId()));
				view.setBrandList(commercialVehicle.getAllVehicleBrandInfo(entity.getVendorId()));
			}
		}
		return view;

	}
	/**
	 * entity -> viewhelper
	 * 
	 * @param entity
	 * @return
	 * @throws TmsDaoException
	 */
	private TransferRgAdvanceInputViewHelper toViewHelper1(TransferPersonalCarInfo entity,TransferAcceptedInfo accept,
			TransferRgAdvanceInputViewHelper view) throws TmsDaoException {
		view.setBrandId(entity.getBrandId());
		view.setVendorId(entity.getVendorId());
		view.setCarStyleId(entity.getCarStyleId());
		view.setCardType(Integer.toString(entity.getCardType()));
		view.setCarType(Integer.toString(entity.getCarType()));
		view.setCarUse(Integer.toString(entity.getCarUse()));
		view.setColor(Integer.toString(entity.getColor()));
		view.setCompanyContactPerson(entity.getCompanyContactPerson());
		if(entity.getCountryStyle() < 0)
		{
			view.setCountryStyle("");
		}else{
			view.setCountryStyle(Integer.toString(entity.getCountryStyle()));
		}
		view.setEngine(entity.getEngine());

		view.setModelCode(entity.getModelCode());
		view.setOwnerAddress(entity.getOwnerAddress());
		view.setOwnerPhone(entity.getOwnerPhone());
		view.setCarOwner(entity.getCarOwner());
		view.setCarOwnerIdCardNo(entity.getCarOwnerIdCardNo());
		view.setOwnerOrgNo(entity.getOwnerOrgNo());
		//modify by huangheli 20150917 start
		Member member = Member.getInstance();
		if(!StringUtil.isNullOrBlank(accept.getMemberId())){
		view.setMemberName(member.getMemNameByMemId(Integer.valueOf(accept.getMemberId())));
		view.setMemberId(accept.getMemberId());
		}
		//modify by huangheli 20150917 end
		view.setRegDate(entity.getRegDate());
		view.setRegisterCardNo(entity.getRegisterCardNo());
		view.setSendCardDate(entity.getSendCardDate());
		view.setVendorCode(entity.getVendorCode());
		view.setVin(entity.getVin());

		if(entity.getVendorId()!=null && !"".equals(entity.getVendorId()))
		{
			//车辆品牌
			Vendor vendor = Vendor.getInstance();
			CarStyle carStyle = CarStyle.getInstance();
			VehicleBrand brand = VehicleBrand.getInstance();
			CommercialVehicle commercialVehicle = CommercialVehicle.getInstance();
			String str = entity.getVendorId().substring(0,1);
			if("S".equals(str))
			{
				view.setFromType(1);
				view.setVendorList(vendor.findAllVendorInfo());
				view.setCarStyleList(carStyle.getAllCarStyleInfo(entity.getBrandId()));
				view.setBrandList(brand.getAllVehicleBrandInfo(entity.getVendorId()));
			}else{
				view.setFromType(2);
				view.setVendorList(commercialVehicle.findAllVendorInfo());
				view.setCarStyleList(commercialVehicle.getAllStyleInfo(entity.getBrandId()));
				view.setBrandList(commercialVehicle.getAllVehicleBrandInfo(entity.getVendorId()));
			}
		}
		return view;
	}

}
